package com.ruoyi.tasks.service.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.api.HouseholdService;
import com.ruoyi.system.api.domain.Household;
import com.ruoyi.tasks.domain.BuildingTasks;
import com.ruoyi.tasks.dto.HouseholdTasksDto;
import com.ruoyi.tasks.dto.TaskDto;
import com.ruoyi.tasks.mapper.BuildingTasksMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tasks.mapper.HouseholdTasksMapper;
import com.ruoyi.tasks.domain.HouseholdTasks;
import com.ruoyi.tasks.service.IHouseholdTasksService;

import static com.ruoyi.tasks.util.AESUtil.decrypt;

/**
 * 住户核酸任务CRUDService业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-05
 */
@Service
public class HouseholdTasksServiceImpl implements IHouseholdTasksService 
{
    @Autowired
    private HouseholdTasksMapper householdTasksMapper;

    @Autowired
    private HouseholdService householdService;

    @Autowired
    private BuildingTasksMapper buildingTasksMapper;

    /**
     * 查询住户核酸任务CRUD
     * 
     * @param id 住户核酸任务CRUD主键
     * @return 住户核酸任务CRUD
     */
    @Override
    public HouseholdTasks selectHouseholdTasksById(Long id)
    {
        HouseholdTasks householdTasks = householdTasksMapper.selectHouseholdTasksById(id);

        Household householdName = householdService.getHouseholdName(householdTasks.getHouseholdName());
        System.out.println(householdName);
        System.out.println("核酸任务即将开始，请您做好准备！！");

        return null;
    }

    /**
     * 查询住户核酸任务CRUD列表
     * 
     * @param householdTasks 住户核酸任务CRUD
     * @return 住户核酸任务CRUD
     */
    @Override
    public List<HouseholdTasks> selectHouseholdTasksList(HouseholdTasks householdTasks)
    {
        return householdTasksMapper.selectHouseholdTasksList(householdTasks);
    }

    /**
     * 新增住户核酸任务CRUD
     * 
     * @param dto 住户核酸任务CRUD
     * @return 结果
     */
    @Override
    public int insertHouseholdTasks(HouseholdTasksDto dto)
    {
        HouseholdTasks tasksName = householdTasksMapper.getTasksName(dto.getTasks());

        HouseholdTasks householdTasks = new HouseholdTasks();
        householdTasks.setTasks(dto.getRqName() + dto.getBuildingName() + dto.getHouseholdName() + dto.getTasks());
        householdTasks.setHouseholdName(dto.getHouseholdName());
        householdTasks.setState("0");
        householdTasks.setStartTime(tasksName.getStartTime());
        householdTasks.setEndTime(tasksName.getEndTime());
        householdTasks.setPosition(tasksName.getPosition());
        return householdTasksMapper.insertHouseholdTasks(householdTasks);
    }

    /**
     * 修改住户核酸任务CRUD
     * 
     * @param householdTasks 住户核酸任务CRUD
     * @return 结果
     */
    @Override
    public int updateHouseholdTasks(HouseholdTasks householdTasks)
    {
        return householdTasksMapper.updateHouseholdTasks(householdTasks);
    }

    /**
     * 批量删除住户核酸任务CRUD
     * 
     * @param ids 需要删除的住户核酸任务CRUD主键
     * @return 结果
     */
    @Override
    public int deleteHouseholdTasksByIds(Long[] ids)
    {
        return householdTasksMapper.deleteHouseholdTasksByIds(ids);
    }

    /**
     * 删除住户核酸任务CRUD信息
     * 
     * @param id 住户核酸任务CRUD主键
     * @return 结果
     */
    @Override
    public int deleteHouseholdTasksById(Long id)
    {
        return householdTasksMapper.deleteHouseholdTasksById(id);
    }

    @Override
    public TaskDto getUrl(TaskDto dto) {
        String url = dto.getUrl();
        try {
            String key = "5F6B2AK33DZE20A0";
            String decode = URLDecoder.decode(url, "utf-8");

            String[] split = decode.split(",");
            System.out.println(Arrays.toString(split));
            String s1 = split[1].toString();
            String s2 = split[3].toString();
            String userId = decrypt(s1, key);
            System.out.println(userId);
            assert userId != null;
            Household household = householdService.getUser(Long.valueOf(userId));

            System.out.println(household);

            TaskDto taskDto = new TaskDto();
            taskDto.setHouseholdName(household.getHouseholdName());
            taskDto.setHouseholdPhone(household.getHouseholdPhone());

            String householdTasksId = decrypt(s2, key);
            assert householdTasksId != null;
            HouseholdTasks householdTasks = householdTasksMapper.selectHouseholdTasksById(Long.valueOf(householdTasksId));

            System.out.println(householdTasks);

            taskDto.setTasks(householdTasks.getTasks());

            return taskDto;
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int updateFinish(TaskDto dto) {

        HouseholdTasks householdTasks = householdTasksMapper.selectBytasks(dto.getTasks());

        householdTasks.setState("2");
        householdTasks.setDetectionTime(new Date());

        String tasks = dto.getTasks();
        String rqName = tasks.substring(0, 4);
        System.out.println(rqName);

        Integer nump = 0;
        BuildingTasks buildingTasks = buildingTasksMapper.getRqName(rqName);
        buildingTasks.setCompleteNumber(nump + 1);
        buildingTasksMapper.updateBuildingTasks(buildingTasks);

        return householdTasksMapper.updateHouseholdTasks(householdTasks);
    }
}
